package coop.main.web;
import coop.main.domain.Detalle;
import coop.main.domain.Maestro;
import coop.main.service.DetalleService;
import coop.main.service.MaestroService;
import coop.main.web.converter.MaestroConverter;
import coop.main.web.util.MessageFactory;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.message.Message;
import org.primefaces.component.outputlabel.OutputLabel;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;

@Configurable
@ManagedBean(name = "detalleBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = Detalle.class, beanName = "detalleBean")
public class DetalleBean implements Serializable {

	@Autowired
    DetalleService detalleService;

	@Autowired
    MaestroService maestroService;

	private String name = "Detalles";

	private Detalle detalle;

	private List<Detalle> allDetalles;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("nombre");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Detalle> getAllDetalles() {
        return allDetalles;
    }

	public void setAllDetalles(List<Detalle> allDetalles) {
        this.allDetalles = allDetalles;
    }

	public String findAllDetalles() {
        allDetalles = detalleService.findAllDetalles();
        dataVisible = !allDetalles.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        return populateViewPanel();
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        javax.faces.application.Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        OutputLabel nombreCreateOutput = (OutputLabel) application.createComponent(OutputLabel.COMPONENT_TYPE);
        nombreCreateOutput.setFor("nombreCreateInput");
        nombreCreateOutput.setId("nombreCreateOutput");
        nombreCreateOutput.setValue("Nombre:");
        htmlPanelGrid.getChildren().add(nombreCreateOutput);
        
        InputText nombreCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        nombreCreateInput.setId("nombreCreateInput");
        nombreCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{detalleBean.detalle.nombre}", String.class));
        nombreCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(nombreCreateInput);
        
        Message nombreCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        nombreCreateInputMessage.setId("nombreCreateInputMessage");
        nombreCreateInputMessage.setFor("nombreCreateInput");
        nombreCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(nombreCreateInputMessage);
        
        OutputLabel maestroCreateOutput = (OutputLabel) application.createComponent(OutputLabel.COMPONENT_TYPE);
        maestroCreateOutput.setFor("maestroCreateInput");
        maestroCreateOutput.setId("maestroCreateOutput");
        maestroCreateOutput.setValue("Maestro:");
        htmlPanelGrid.getChildren().add(maestroCreateOutput);
        
        AutoComplete maestroCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        maestroCreateInput.setId("maestroCreateInput");
        maestroCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{detalleBean.detalle.maestro}", Maestro.class));
        maestroCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{detalleBean.completeMaestro}", List.class, new Class[] { String.class }));
        maestroCreateInput.setDropdown(true);
        maestroCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "maestro", String.class));
        maestroCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{maestro.nombre}", String.class));
        maestroCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{maestro}", Maestro.class));
        maestroCreateInput.setConverter(new MaestroConverter());
        maestroCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(maestroCreateInput);
        
        Message maestroCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        maestroCreateInputMessage.setId("maestroCreateInputMessage");
        maestroCreateInputMessage.setFor("maestroCreateInput");
        maestroCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(maestroCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        javax.faces.application.Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        OutputLabel nombreEditOutput = (OutputLabel) application.createComponent(OutputLabel.COMPONENT_TYPE);
        nombreEditOutput.setFor("nombreEditInput");
        nombreEditOutput.setId("nombreEditOutput");
        nombreEditOutput.setValue("Nombre:");
        htmlPanelGrid.getChildren().add(nombreEditOutput);
        
        InputText nombreEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        nombreEditInput.setId("nombreEditInput");
        nombreEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{detalleBean.detalle.nombre}", String.class));
        nombreEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(nombreEditInput);
        
        Message nombreEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        nombreEditInputMessage.setId("nombreEditInputMessage");
        nombreEditInputMessage.setFor("nombreEditInput");
        nombreEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(nombreEditInputMessage);
        
        OutputLabel maestroEditOutput = (OutputLabel) application.createComponent(OutputLabel.COMPONENT_TYPE);
        maestroEditOutput.setFor("maestroEditInput");
        maestroEditOutput.setId("maestroEditOutput");
        maestroEditOutput.setValue("Maestro:");
        htmlPanelGrid.getChildren().add(maestroEditOutput);
        
        AutoComplete maestroEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        maestroEditInput.setId("maestroEditInput");
        maestroEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{detalleBean.detalle.maestro}", Maestro.class));
        maestroEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{detalleBean.completeMaestro}", List.class, new Class[] { String.class }));
        maestroEditInput.setDropdown(true);
        maestroEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "maestro", String.class));
        maestroEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{maestro.nombre}", String.class));
        maestroEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{maestro}", Maestro.class));
        maestroEditInput.setConverter(new MaestroConverter());
        maestroEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(maestroEditInput);
        
        Message maestroEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        maestroEditInputMessage.setId("maestroEditInputMessage");
        maestroEditInputMessage.setFor("maestroEditInput");
        maestroEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(maestroEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        javax.faces.application.Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText nombreLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombreLabel.setId("nombreLabel");
        nombreLabel.setValue("Nombre:");
        htmlPanelGrid.getChildren().add(nombreLabel);
        
        HtmlOutputText nombreValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombreValue.setId("nombreValue");
        nombreValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{detalleBean.detalle.nombre}", String.class));
        htmlPanelGrid.getChildren().add(nombreValue);
        
        HtmlOutputText maestroLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        maestroLabel.setId("maestroLabel");
        maestroLabel.setValue("Maestro:");
        htmlPanelGrid.getChildren().add(maestroLabel);
        
        HtmlOutputText maestroValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        maestroValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{detalleBean.detalle.maestro}", Maestro.class));
        maestroValue.setConverter(new MaestroConverter());
        htmlPanelGrid.getChildren().add(maestroValue);
        
        return htmlPanelGrid;
    }

	public Detalle getDetalle() {
        if (detalle == null) {
            detalle = new Detalle();
        }
        return detalle;
    }

	public void setDetalle(Detalle detalle) {
        this.detalle = detalle;
    }

	public List<Maestro> completeMaestro(String query) {
        List<Maestro> suggestions = new ArrayList<Maestro>();
        for (Maestro maestro : maestroService.findAllMaestroes()) {
            String maestroStr = String.valueOf(maestro.getNombre());
            if (maestroStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(maestro);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllDetalles();
        return "detalle";
    }

	public String displayCreateDialog() {
        detalle = new Detalle();
        createDialogVisible = true;
        return "detalle";
    }

	public String persist() {
        String message = "";
        if (detalle.getId() != null) {
            detalleService.updateDetalle(detalle);
            message = "message_successfully_updated";
        } else {
            detalleService.saveDetalle(detalle);
            message = "message_successfully_created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialogWidget.hide()");
        context.execute("editDialogWidget.hide()");
        
        FacesMessage facesMessage = MessageFactory.getMessage(message, "Detalle");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllDetalles();
    }

	public String delete() {
        detalleService.deleteDetalle(detalle);
        FacesMessage facesMessage = MessageFactory.getMessage("message_successfully_deleted", "Detalle");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllDetalles();
    }

	public void reset() {
        detalle = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }

	private static final long serialVersionUID = 1L;
}
